import sys
import numpy as np
from PyQt5 import QtWidgets
import pyqtgraph as pg
import pyqtgraph.opengl as gl

from PyQt5.QtGui import QColor, QOpenGLVersionProfile
from PyQt5.QtWidgets import (QApplication, QWidget, QOpenGLWidget, QHBoxLayout)


from qt_ui import Ui_MainWindow


class MyGraphWindow(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):

        super(MyGraphWindow, self).__init__()
        self.setupUi(self)  # 初始化窗口

        pg.setConfigOptions(antialias=True)#
        #pg.useOpenGL()
        self.win = pg.GraphicsLayoutWidget()
        # self.graph_layout.addWidget(self.win)

        #self.p1, self.p2 = self.set_graph_ui()  # 设置绘图窗口

        self.btn.clicked.connect(self.plot_sin_cos)


        # 点击按键开始绘图
    def Plot_3d(self):
        self.win.clear()  # 清除图像
        glWidget=MyGLWidget()
        mainLayout=QHBoxLayout()
        mainLayout.addWidget(glWidget)
        self.graph_layout.addWidget(mainLayout)


    def plot_sin_cos(self):
        self.graph_layout.addWidget(self.win)
        self.win.clear()#清除图像
        p1 = self.win.addPlot(title="sin 函数")  # 添加第一个sin绘图窗口

        p1.setLabel('left', text='meg', color='#ffffff')  # y轴设置函数
        p1.showGrid(x=True, y=True)  # 栅格设置函数
        p1.setLogMode(x=False, y=False)  # False代表线性坐标轴，True代表对数坐标轴
        p1.setLabel('bottom', text='time', units='s')  # x轴设置函数
        self.win.nextRow()  # layout换行，采用垂直排列，不添加此行则默认水平排列

        p2 = self.win.addPlot(title="cos 函数") # 添加第二个cos绘图窗口
        p2.setLabel('left', text='meg', color='#ffffff')
        p2.showGrid(x=True, y=True)
        p2.setLogMode(x=False, y=False)
        p2.setLabel('bottom', text='time', units='s')
        t = np.linspace(0, 20, 200)
        y_sin = np.sin(t)
        y_cos = np.cos(t)
        p1.plot(t, y_sin, pen='g', name='sin(x)', clear=True)
        p2.plot(t, y_cos, pen='g', name='con(x)', clear=True)



class MyGLWidget(QOpenGLWidget):

    def __init__(self, parent=None):
        super(MyGLWidget, self).__init__(parent)

    def initializeGL(self):
        version_profile = QOpenGLVersionProfile()
        version_profile.setVersion(2, 0)
        self.gl = self.context().versionFunctions(version_profile)
        self.gl.initializeOpenGLFunctions()

        # 设置背景色
        self.gl.glClearColor(0.2, 0.4, 0.52, 1.0)
        # 深度测试
        self.gl.glEnable(self.gl.GL_DEPTH_TEST)

    def paintGL(self):
        self.gl.glClear(self.gl.GL_COLOR_BUFFER_BIT | self.gl.GL_DEPTH_BUFFER_BIT)
        self.gl.glLoadIdentity()

        # self.gl.glRotated(30.0, 1.0, 0.0, 0.0)
        self.gl.glBegin(self.gl.GL_TRIANGLES)
        self.gl.glColor3d(1.0, 0.0, 0.0)
        self.gl.glVertex3d(0.0, 1.0, 0.0)
        self.gl.glColor3d(0.0, 1.0, 0.0)
        self.gl.glVertex3d(-1.0, -1.0, 0.0)
        self.gl.glColor3d(0.0, 0.0, 1.0)
        self.gl.glVertex3d(1.0, -1.0, 0.0)
        self.gl.glEnd()

    def resizeGL(self, width, height):
        side = min(width, height)
        if side < 0:
            return

        # 视口
        self.gl.glViewport((width - side) // 2, (height - side) // 2, side, side)
        self.gl.glMatrixMode(self.gl.GL_PROJECTION)
        self.gl.glLoadIdentity()
        # 正交投射
        self.gl.glOrtho(-1.5, 1.5, -1.5, 1.5, -10, 10)
        self.gl.glMatrixMode(self.gl.GL_MODELVIEW)






if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    myWin = MyGraphWindow()
    myWin.show()
    sys.exit(app.exec_())

